/**
 * Created by Administrator on 2016/1/21.
 */

define("wangwu/sxyc/branchCanal",
    [
        'dojo/_base/declare',
        'dojo/_base/lang',
        'dojo/on',
        'dojo/dom-construct',
        'dojo/query',

        'esri/geometry/Point',
        'esri/geometry/Polyline',
        'esri/layers/GraphicsLayer',
        'esri/graphic',
        'esri/symbols/SimpleMarkerSymbol',
        'esri/symbols/SimpleLineSymbol',
        'esri/Color'

    ],
    function(declare,lang, on,domConstruct,djQuery,
             Point,Polyline,GraphicsLayer,Graphic,SimpleMarkerSymbol,SimpleLineSymbol,Color){
        var d = declare(null,{
            declaredClass:'wangwu.sxyc.branchCanal',
            constructor:function(map,opt){
                this.map = map;
                this.option={
                };
                (opt) ? lang.mixin(this.option, opt) : 0;
                this.id = 'zhiqu'+parseInt(Math.random()*1000);
                this.layer = new GraphicsLayer({
                    id:this.id+'_lyr'
                });
                this.map.addLayer(this.layer);
                this.initialized = false;
                var mouseOver = on(this.layer, 'mouse-over', lang.hitch(this, this._mouseOver));
                var mouseOut = on(this.layer, 'mouse-out', lang.hitch(this, this._mouseOut));
                //调试用
                //this.buttonId = 'testDojo-'+parseInt(Math.random()*1000);
                //domConstruct.create('div', {
                //    'id':'aaaa1111',
                //    'style':'position:absolute;right:20px;top:130px;width:100px',
                //    'innerHTML':'<input type="button" value="添加dom" id="'+this.buttonId+'"/>'
                //}, map.id);
                //var button = djQuery('#'+this.buttonId);
                //on(button, 'click', lang.hitch(this, this.init));
            },
            //-----------------------public----------------------------------

            init:function(){
                if (this.initialized) return;
                this.initialized = true;
                //this._init();
            },
            setDataSource:function(data){
                this.dataSource = data;
                this._init();
            },
            //-----------------------private----------------------------------
            _init:function(){
                var data = this.dataSource;
                this.layer.clear();
                for (var i=0; i<data.length;i++){
                    var di = data[i];
                    var g = this._create(di);
                    this.layer.add(g);
                }
            },

            _create:function(di){
                var geo = di.geo;
                var attr = di.attr;
                //console.log(geo);
                //console.log(attr);
                var path = geo.paths[0];
                var symbol = new SimpleLineSymbol('solid',new Color([70,77,236,0.8]),3);
                var line = new Polyline(this.map.spatialReference);
                line.addPath(path);
                var graphic = new Graphic(line,symbol,attr);
                //console.log(graphic);
                return graphic;
            },
            //--------------------------------pre event---------------------------------
            _graphicClick:function(evt){
                this.onGraphicClick(evt);
            },
            _mouseOver:function(e){
                if(e.graphic.symbol){
                    switch(e.graphic.symbol.declaredClass){
                        case "esri.symbol.SimpleLineSymbol":
                            e.graphic.symbol.setWidth(e.graphic.symbol.width*1.5);
                            break;
                        case "esri.symbol.PictureMarkerSymbol":
                            e.graphic.symbol.setWidth(e.graphic.symbol.width*1.5);
                            e.graphic.symbol.setHeight(e.graphic.symbol.height*1.5);
                            break;
                        case "esri.symbol.SimpleMarkerSymbol":
                            e.graphic.symbol.setSize(e.graphic.symbol.size*1.5);
                            break;
                    }
                }
                this.layer.redraw();
                this.onMouseOver();
            },
            _mouseOut:function(e){
                if(e.graphic.symbol){
                    switch(e.graphic.symbol.declaredClass){
                        case "esri.symbol.SimpleLineSymbol":
                            e.graphic.symbol.setWidth(e.graphic.symbol.width/1.5);
                            break;
                        case "esri.symbol.PictureMarkerSymbol":
                            e.graphic.symbol.setWidth(e.graphic.symbol.width/1.5);
                            e.graphic.symbol.setHeight(e.graphic.symbol.height/1.5);
                            break;
                        case "esri.symbol.SimpleMarkerSymbol":
                            e.graphic.symbol.setSize(e.graphic.symbol.size/1.5);
                            break;
                    }
                }
                this.layer.redraw();
                this.onMouseOut();
            },
            //--------------------------------event---------------------------------------
            onGraphicClick:function(evt){

            },
            onMouseOver:function(){

            },
            onMouseOut:function(){

            }
        });

        return d;
    })